package com.example.cggame;

import java.util.ArrayList;
import java.util.Random;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.*;

public class GameState {
	
	int score;
    Random rand = new Random();

	//screen width and height
	final int screenWidth = 300;
	final int screenHeight = 420;
	
	private ArrayList<Ball> ballList = new ArrayList<Ball>();

	//Ball ball = new Ball(screenWidth);

	Bat bat = new Bat(screenWidth);

	public GameState(){
		score = 0;
		// Making list of simultaneous balls
		ballList.add(new Ball(5, 6, screenWidth));
		ballList.add(new Ball(0, 5, screenWidth));
		ballList.add(new Ball(1, 5, screenWidth));
		ballList.add(new Ball(0, 7, screenWidth));
		ballList.add(new Ball(2, 8, screenWidth));
		ballList.add(new Ball(4, 2, screenWidth));
		
		// Making list of paint, colors for the balls
		Paint paint1 = new Paint();
		paint1.setARGB(200, 100, 0, 100);
		paintList.add(paint1);
		Paint paint2 = new Paint();
		paint2.setARGB(200, 0, 0, 200);
		paintList.add(paint2); 
		Paint paint3 = new Paint();
		paint3.setARGB(200, 200, 200, 0);
		paintList.add(paint3);
		Paint paint4 = new Paint();
		paint4.setARGB(200, 200, 0, 0);
		paintList.add(paint4); 
		Paint paint5 = new Paint();
		paint5.setARGB(200, 200, 200, 200);
		paintList.add(paint5); 
		
		// Adding a random color for a single ball
		for(Ball ball : ballList){
			int r = rand.nextInt(paintList.size());
			ball.setBallColor(paintList.get(r));
		}
	}
	
	public static boolean BallBatCollision(Ball ball, Bat bat) {
	    if(ball.getBallX() < bat.getBatX() + bat.getBatLength() &&
	       ball.getBallX() + ball.getBallSize() > bat.getBatX() &&
	       ball.getBallY() < bat.getBatY() + bat.getBatHeight() &&
	       ball.getBallY() + ball.getBallSize() > bat.getBatY())
	        return true;
	    else
	        return false;
	}
	
	public Ball updateBall(Ball ball){
		ball.setBallX(ball.getBallX() + ball.getBallVelocityX());
		ball.setBallY(ball.getBallY() + ball.getBallVelocityY());

		//DEATH! Ball wasn't hit by bat and passes the bottom of screen
		if(ball.getBallY() > screenHeight){
			ball.setBallX(rand.nextInt(screenWidth)); 	
			ball.setBallY(0);
			score--; // Lost the Ball -1 point, sorry!
			// Game ends?
		}
		
		//Ball hit by bat and passed top of screen
		if(ball.getBallY() < 0){
			ball.setBallX(rand.nextInt(screenWidth));	
			ball.setBallY(0);
			score++;	// Bat hit the ball, +1 point congratulations!
			// Reversed trajectory so next ball will go down to bat again
			ball.setBallVelocityY(ball.getBallVelocityY() * -1);
		}
		
		//Collisions with the sides
		if(ball.getBallX() > screenWidth || ball.getBallX() < 0)
			ball.setBallVelocityX(ball.getBallVelocityX() * -1); // Ball hit the side ball inverting trajectory 	     	
		
		//Collisions with the bat
		// If collision with bat happens ball trajectory is reverted to other side
		if(BallBatCollision(ball, bat)){
			ball.setBallVelocityY(ball.getBallVelocityY() * -1); // Ball going up
		}
		return ball;
	}

	//The update method
	public void update() {
		
		for(Ball ball : ballList){
			ball = updateBall(ball);
		}
	}
	

	public boolean keyPressed(int keyCode, KeyEvent msg)
	{
		// Keyboard event for Left key
		if(keyCode == KeyEvent.KEYCODE_DPAD_LEFT) //left
		{
			if(!(bat.getBatX() < 0 ))
				bat.setBatX(bat.getBatX() - bat.getBatSpeed());
		}

		// Keyboard event for Right key
		if(keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) //right
		{
			if(!(bat.getBatX() + bat.getBatLength() > screenWidth))
				bat.setBatX(bat.getBatX() + bat.getBatSpeed());
		}

		return true;
	}
	
	
	private ArrayList<Paint> paintList = new ArrayList<Paint>();

	//the draw method
	public void draw(Canvas canvas, Paint paint) {

		//Clear the screen
		canvas.drawRGB(20, 20, 20);
		
		//Draw the Score
		paint.setARGB(200, 200, 200, 200); // Score White
		canvas.drawText("Score: " + score, 1, 10, paint);
		
		for(Ball ball : ballList){
		
			//draw the ball
			int r = rand.nextInt(paintList.size());
			canvas.drawRect(new Rect(ball.getBallX(),ball.getBallY(),ball.getBallX() + ball.getBallSize(),
					ball.getBallY() + ball.getBallSize()), paintList.get(r) /*(ball.getBallColor()*/);
		}
		
		//set the colour
		paint.setARGB(200, 0, 200, 0); // Bat Green
				
		//draw the bat
		canvas.drawRect(new Rect(bat.getBatX(), bat.getBatY(), bat.getBatX() + bat.getBatLength(), 
				bat.getBatY() + bat.getBatHeight()), paint);

	}
}
